

food.controller('LinkListConroller',function($http,$scope,page){
 

 // 获取友情链接整张表的总数
  $http({
    
    method:'get',
    url:'../Api/admin.php?action=link_count'
  }).then(function(res){
    $scope.totalItems=res.data.c;
  });

  // 组装分页

 $scope.currentPage=1;
 $scope.bigTotalItems=8;
 $scope.maxSize=5;
 var url="../Api/admin.php?action=link_list";
 page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(res){
   $scope.link_list=res;
 });

$scope.pageChanged=function(){
    page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(res){
   $scope.link_list=res;
 });

};

});

food.controller('LinkAddConroller',function($http,$location,$scope,Upload){
 
// 提交图片的方法

   $scope.uploadImg = function()
    {
        if($scope.link.link_img){
            // 返回文件名称
            console.log($scope.link.link_img);
            // 进度条插件
            Upload.upload({
                url:"../Api/admin.php?action=link_add",//返回图片路径 {'img':'404.jpg'}
                file:$scope.link.link_img
            }).success(function(result){
                $scope.link.link_img = "uploads/"+result.link_img;//存到本地文件夹
            });
        }
    };


// 表单提交信息

$scope.saveForm = function(){

        $http({
            method:"POST",
            url:"../Api/admin.php?action=link_add",
            data:$scope.link,

            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){
                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }
                return str.join("&");
            }
        }).then(function(result){

            if(result.data){
                $location.path('link_list');
            }else{
                alert('添加失败');
                $location.path('link_list');
            }
        });
    }



});

food.controller('LinkEditeConroller',function($http,$location,$scope,$stateParams,Upload){

  // 显示旧信息
 
  $http({
    
    method:'get',
    url:'../Api/admin.php?action=link_detail&link_id='+$stateParams.link_id

  }).then(function(res){

    $scope.link=res.data;

    
  });
  
// 修改文件
 
   $scope.uploadImg = function(){

        if($scope.link.link_img){

            // 返回文件名称
            console.log($scope.link.link_img);

            // 进度条插件
            Upload.upload({

                url:"../Api/admin.php?action=link_edite",//返回图片路径 {'img':'404.jpg'}
                file:$scope.link.link_img

            }).success(function(result){

                $scope.link.link_img = "uploads/"+result.link_img;//存到本地文件夹
                console.log($scope.link.link_img);
            });
        }
    };



// 修改数据

 $scope.saveForm = function(){
        $http({
            method:"POST",
            url:"../Api/admin.php?action=link_edite&link_id="+$stateParams.link_id,
            data:$scope.link,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj)
                {
                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }
        }).then(function(result){
            if(result.data)
            {
                $location.path('link_list');
            }else{
                alert('添加失败');
                $location.path('link_list');
            }
        });
    }
});

food.controller('LinkDeleteConroller',function($http,$scope,$location,$stateParams){
   $http({
     method:'get',
     url:'../Api/admin.php?action=link_delete&link_id='+$stateParams.link_id
   }).then(function(res){
      if(res.data){
        $location.path("link_list");
      }else{
         alert("删除失败");
        $location.path("link_list");
      }
   });
});

